package com.xii.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xii.constant.MessageConstant;
import com.xii.entity.PageResult;
import com.xii.entity.QueryPageBean;
import com.xii.entity.Result;
import com.xii.pojo.CheckItem;
import com.xii.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * @description: 新增
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")      //权限效验
    @ResponseBody
    public Result add(@RequestBody CheckItem checkItem){

        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }
    /**
     * @description: 分页查询  定义返回对象
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @PostMapping("/findPage")
//    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")      //权限效验
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return checkItemService.findPage(pageBean);
    }


    /**
     * @description: 删除检查项
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @GetMapping("delete")
//    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")      //权限效验
    @ResponseBody
    public Result delete(Integer id){
        try {
            checkItemService.delete(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    /**
     * @description: 通过id查询
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @GetMapping("findById")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")      //权限效验   授权
    @ResponseBody
    public Result findById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * @description: 编辑修改
     *
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @PostMapping("edit")
    @ResponseBody
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }



    @GetMapping("findAll")
    @ResponseBody
    public Result findAll(){
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItemList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
